{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Longest Substring With K Distinct Characters\n",
    "\n",
    "[原题](https://mp.weixin.qq.com/s/cABge9jKkEAfrUK7yelHtA)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "You are given a string s, and an integer k. Return the length of the longest substring in s that contains at most k distinct characters."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "For the given string `aabcdefff` and the integer `3`, then the longest substring with 3 distinct characters would be `defff`. The answer should be `5`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def longest_substring_with_k_distinct_characters(s, k):\n",
    "    ''' Algorithm: Moving Window '''\n",
    "\n",
    "    # Use 2 queue to realize Moving Window and record the longest one.\n",
    "    # NOTES: DON'T use an even equation to define them,\n",
    "    # if so, their object id are the same and update together.\n",
    "    window = []\n",
    "    result = []\n",
    "\n",
    "    # Traverse all characters.\n",
    "    for char in s:\n",
    "        # Append it to the current window.\n",
    "        window.append(char)\n",
    "        \n",
    "        # Check if there're more unique characters than requirement.\n",
    "        while len(set(window)) > k:\n",
    "            # Remove the first character of the current window.\n",
    "            window.remove(window[0])\n",
    "        \n",
    "        # Update the longest one.\n",
    "        if len(window) > len(result):\n",
    "            # NOTES: DON'T use assignment!\n",
    "            # If so, their object id will be the same and update together.\n",
    "            result = window.copy()\n",
    "            \n",
    "    # Return the longest substring and its length.\n",
    "    return (''.join(result), len(result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('aabbcc', 6)\n"
     ]
    }
   ],
   "source": [
    "print(longest_substring_with_k_distinct_characters('aabbccdefff', 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Notes\n",
    "\n",
    "The built-in data type `list` is an available type of objects. When assign an object to another, it assign their **Object ID** (you can regard them as *Pointer*) instead of their value. So, when initializing, they should initialize seperately. Also use class method `copy()` to assign their values, copy value from an object to another."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
